﻿/****************************** Module Header ******************************\
 * Module Name:  _Request.cs
 * Module Version:  1.0.0 (2012-9-16)
 * Copyright (c) AnyEB.Com (14507247@qq.com)
 * 
 * This class is used for _Request.
 * 
 * This source is subject to the GPL License.
 * See http://www.gnu.org/licenses/gpl.html.
 * All other rights reserved.
 * 
 * THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, 
 * EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED 
 * WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
\***************************************************************************/
using System;
using System.Linq;
using System.Web;

namespace UCF.Core.Web
{
    public class _Request
    {
        public static HttpRequest Request { get { return HttpContext.Current.Request; } }
        #region Url和Form
        public static bool HasVar(string name)
        {
            return HasUrlVar(name) || HasFormVar(name);
        }
        public static string GetString(string name)
        {
            if (Request.QueryString.AllKeys.Contains(name))
            {
                return Request.QueryString[name];
            }
            if (Request.Form.AllKeys.Contains(name))
            {
                return Request.Form[name];
            }
            return string.Empty;
        }
        public static bool GetBool(string name)
        {
            return GetString(name).ToBoolean();
        }
        public static int GetInt(string name)
        {
            return GetString(name).ToInt();
        }
        public static decimal GetDecimal(string name)
        {
            return GetString(name).ToDecimal();
        }
        public static short GetShort(string name)
        {
            return GetString(name).ToShort();
        }
        public static double GetDouble(string name)
        {
            return GetString(name).ToDouble();
        }
        public static Guid GetGuid(string name)
        {
            return GetString(name).ToGuid();
        }
        public static DateTime GetDateTime(string name)
        {
            return GetString(name).ToDateTime();
        }

        #endregion
        #region Url变量
        public static bool HasUrlVar(params string[] names)
        {
            return InKeys(Request.QueryString.AllKeys, names);
        }
        public static string UrlString(string name, string defv = "")
        {
            return HasUrlVar(name) ? Request.QueryString[name] : defv;
        }
        public static bool UrlBool(string name)
        {
            return UrlString(name).ToBoolean();
        }
        public static int UrlInt(string name)
        {
            return UrlString(name).ToInt();
        }
        public static decimal UrlDecimal(string name)
        {
            return UrlString(name).ToDecimal();
        }
        public static short UrlShort(string name, short defv = 0)
        {
            return UrlString(name).ToShort(defv);
        }
        public static double UrlDouble(string name)
        {
            return UrlString(name).ToDouble();
        }
        public static Guid UrlGuid(string name)
        {
            return UrlString(name).ToGuid();
        }
        public static DateTime UrlDateTime(string name)
        {
            return UrlString(name).ToDateTime();
        }

        #endregion
        #region Form变量
        private static bool InKeys(string[] keys, params string[] names)
        {
            foreach (string s in names)
            {
                if (!keys.Contains(s)) return false;
            }
            return true;
        }
        public static bool HasFormVar(params string[] names)
        {
            return InKeys(Request.Form.AllKeys, names);
        }
        public static string FormString(string name)
        {
            return HasFormVar(name) ? Request.Form[name] : string.Empty;
        }
        public static bool FormBool(string name)
        {
            return FormString(name).ToBoolean();
        }
        public static int FormInt(string name)
        {
            return FormString(name).ToInt();
        }
        public static decimal FormDecimal(string name)
        {
            return FormString(name).ToDecimal();
        }
        public static short FormShort(string name)
        {
            return FormString(name).ToShort();
        }
        public static double FormDouble(string name)
        {
            return FormString(name).ToDouble();
        }
        public static Guid FormGuid(string name)
        {
            return FormString(name).ToGuid();
        }
        public static DateTime FormDateTime(string name)
        {
            return FormString(name).ToDateTime();
        }

        #endregion

        public static string RequestString(string name,string defv="",bool emptyAsNull=false,bool escape=true)
        {
             return (Request[name] == null || (emptyAsNull && string.IsNullOrEmpty(Request[name]))) ? defv : (escape?Request[name].Replace("'",""):Request[name]);
            
        }
        public static int RequestInt(string name, int defv = 0)
        {
            int v = RequestString(name).ToInt();
            return v == 0 ? defv : v;
        }
    }
}
